/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "textfindwidget.h"
#include <QtGui/QIcon>
#include <QtGui/QPushButton>
#include <QtGui/QLineEdit>
#include <QtGui/QCheckBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QLabel>
#include <QtCore/QEvent>
#include "src/includes/platform.h"

TextFindWidget::TextFindWidget(QWidget *parent) :
    QWidget(parent)
{
    createUi();
    connect(findButton, SIGNAL(clicked()),this,SLOT(whenFindButtonClicked()));
}


TextFindWidget::~TextFindWidget() {
    delete caseSensetiveCheckBox;
    delete wholeWordsCheckBox;
    delete regexpCheckBox;
    delete findBackwardCheckBox;
    delete checkboxesLayout;
    delete textLabel;
    delete findTextLineEdit;
    delete findButton;
    delete findTextLayout;
    delete mainLayout;
}

QTextDocument::FindFlags TextFindWidget::getFindFlags() /*const*/ {
    bool cs = caseSensetiveCheckBox->isChecked();
    bool ww = wholeWordsCheckBox->isChecked();
    bool bf = findBackwardCheckBox->isChecked();

    QTextDocument::FindFlags flags = 0;

    cs && (flags |= QTextDocument::FindCaseSensitively);
    ww && (flags |= QTextDocument::FindWholeWords);
    bf && (flags |= QTextDocument::FindBackward);
    return flags;
}

QString TextFindWidget::getFindText() const {
    return findTextLineEdit->text();
}


bool TextFindWidget::isUseRegexp() const {
    return regexpCheckBox->isChecked();
}



void TextFindWidget::createUi() {
    caseSensetiveCheckBox = new QCheckBox(this);
    wholeWordsCheckBox = new QCheckBox(this);
    regexpCheckBox = new QCheckBox(this);
    findBackwardCheckBox = new QCheckBox(this);

    checkboxesLayout = new QHBoxLayout;

    checkboxesLayout->addWidget(caseSensetiveCheckBox);
    checkboxesLayout->addWidget(wholeWordsCheckBox);
    checkboxesLayout->addWidget(regexpCheckBox);
    checkboxesLayout->addWidget(findBackwardCheckBox);
    checkboxesLayout->addStretch();
    //! TODO: insert spacer correctly


    textLabel = new QLabel(this);
    findTextLineEdit = new QLineEdit(this);
    findButton = new QPushButton(this);

#ifdef QP_FDOICONS_SUPPORT
    findButton->setIcon(QIcon::fromTheme("edit-find"));
#endif

    findTextLayout = new QHBoxLayout;

    findTextLayout->addWidget(textLabel);
    findTextLayout->addWidget(findTextLineEdit);
    findTextLayout->addWidget(findButton);

    mainLayout = new QVBoxLayout;

    mainLayout->addLayout(checkboxesLayout);
    mainLayout->addLayout(findTextLayout);


    this->setLayout(mainLayout);
}

void TextFindWidget::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	textLabel->setText(QLabel::tr("Text"));
        findTextLineEdit->setPlaceholderText(QLineEdit::tr("Input query here"));
    }
}

void TextFindWidget::whenFindButtonClicked() {
    const QString text = getFindText();
    if(!text.isEmpty())
	emit findRequested(text, getFindFlags(), isUseRegexp());
}
